import { Component, Input, OnInit, ViewChild }                   from '@angular/core';
import { NzMessageService, NzModalRef }                          from 'ng-zorro-antd';
import { SFSchema, SFUISchemaItem }                              from '@delon/form';
import { City }                                                  from "@shared/model/city";
import { map }                                                   from "rxjs/operators";
import { of }                                                    from "rxjs";
import { STChange, STColumn, STComponent, STData, STReq, STRes } from "@delon/abc";
import { CacheService }                                          from "@delon/cache";
import { UserService }                                           from "@core/user/user.service";
import { Bonus }                                                 from "@shared/model/bonus";

@Component({
    selector   : 'app-device-view-bonus-staff-list',
    templateUrl: './staff-list.component.html',
})
export class DeviceViewBonusStaffListComponent implements OnInit
{
    
    @Input()
    deviceId: string;
    @Input()
    selected: Array<Bonus>;
    
    searchSchema: SFSchema = {
        properties: {
            userName: {
                type : 'string',
                title: '联系人',
            },
        },
    };
    selectedRows = [];
    ui: SFUISchemaItem = {
        "$address.city.id": {
            widget   : 'select',
            width    : 180,
            asyncData: () =>
            {
                return this.pid ? this.cacheService.get<City[]>(`api/cities?parentId=${ this.pid }`)
                    .pipe(map(value => value.map(i => ({
                        value: i.id,
                        label: i.name,
                    })))) : of([]);
            },
        },
        $name             : {}
    };
    @ViewChild('st', { static: false }) st: STComponent;
    columns: STColumn[] = [
        { title: '', type: "checkbox", index: 'id' },
        // { title: '调用次数', type: 'number', index: 'callNo' },
        { title: '', type: 'img', width: '50px', index: 'imageUrl' },
        // { title: '时间', type: 'date', index: 'updatedAt' },
        { title: '省份', index: 'provinceName' },
        { title: '城市', index: 'cityName' },
        { title: '加入时间', index: 'createdDate', type: 'date' },
        { title: '联系人', index: 'userName' },
        { title: '联系人电话', index: 'phone' },
        { title: '信息介绍', index: 'description' },
    ];
    private pid: any;
    
    req: STReq;
    url: string;
    res: STRes = {
        process: ( data: STData[] ) => data.map(( value, index ) =>
        {
            value.disabled = !!this.selected.find(i => i.userId === value.id);
            return value;
        })
    };
    
    constructor(
        private modal: NzModalRef,
        private msgSrv: NzMessageService,
        private cacheService: CacheService, public userService: UserService
    )
    {
    }
    
    ngOnInit(): void
    {
        this.req = {
            params: {
                sort    : 'createdDate,desc',
                status  : 1,
                deviceId: this.deviceId
            }
        };
        this.url = `api/users/staffs`;
    }
    
    save()
    {
        console.log(this.selectedRows);
        this.modal.destroy(this.selectedRows)
    }
    
    close()
    {
        this.modal.destroy();
    }
    
    stChange( change: STChange )
    {
        switch (change.type)
        {
            case "pi":
                break;
            case "ps":
                break;
            case "checkbox":
                this.selectedRows = change.checkbox!;
                break;
            case "radio":
                break;
            case "sort":
                break;
            case "filter":
                break;
            case "click":
                break;
            case "dblClick":
                break;
            case "expand":
                break;
        }
    }
    
    dataProcess( data: STData[] )
    {
        return data.map(( i: STData, index: number ) =>
        {
        
        });
    }
}
